package com.mxpio.erp.technology.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.technology.entity.ItemPurchaseProp;
import com.mxpio.erp.common.technology.entity.ItemPropKey;
import com.mxpio.erp.common.technology.service.ItemPurchasePropService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.CriteriaUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "ItemPurchasePropController", tags = { "物料采购属性" })
@RestController
@RequestMapping("/erp/common/item/prop/purchase")
public class ItemPurchasePropController {
	
	@Autowired
	private ItemPurchasePropService itemPurchasePropSerivce;

	@GetMapping("list")
	@ApiOperation(value = "物料采购属性列表", notes = "获取物料采购属性列表", httpMethod = "GET")
	public Result<List<ItemPurchaseProp>> list(String criteria) throws UnsupportedEncodingException {
		Criteria c = CriteriaUtils.json2Criteria(criteria);
		List<ItemPurchaseProp> itemPurchaseProps = itemPurchasePropSerivce.list(ItemPurchaseProp.class, c);
		return Result.OK(itemPurchaseProps);
	}
	
	@GetMapping("page")
	@ApiOperation(value = "物料采购属性列表", notes = "获取物料采购属性列表", httpMethod = "GET")
	public Result<Page<ItemPurchaseProp>> page(String criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable page = PageRequest.of(pageNo-1, pageSize);
		Criteria c = CriteriaUtils.json2Criteria(criteria);
		Page<ItemPurchaseProp> itemPurchaseProps = itemPurchasePropSerivce.listPage(ItemPurchaseProp.class, page, c);
		return Result.OK(itemPurchaseProps);
	}
	
	@GetMapping("list/{propType}/{code}")
	@ApiOperation(value = "根据type，code获取物料采购属性", notes = "根据type，code获取物料采购属性", httpMethod = "GET")
	public Result<ItemPurchaseProp> getById(@PathVariable(name = "propType", required = true) String propType,
			@PathVariable(name = "code", required = true) String code) {
		ItemPropKey key = new ItemPropKey();
		key.setPropType(propType);
		key.setCode(code);
		ItemPurchaseProp itemPurchaseProp = itemPurchasePropSerivce.getById(ItemPurchaseProp.class, code);
		return Result.OK(itemPurchaseProp);
	}
	
	@PostMapping("add")
	@ApiOperation(value = "新增物料采购属性", notes = "新增物料采购属性", httpMethod = "POST")
	public Result<ItemPurchaseProp> add(@RequestBody ItemPurchaseProp itemPurchaseProp) {
		itemPurchasePropSerivce.save(itemPurchaseProp);
		return Result.OK(itemPurchaseProp);
	}
	
	@PutMapping("edit")
	@ApiOperation(value = "编辑物料采购属性", notes = "编辑物料采购属性（全量）", httpMethod = "PUT")
	public Result<ItemPurchaseProp> edit(@RequestBody ItemPurchaseProp itemPurchaseProp) {
		itemPurchasePropSerivce.update(itemPurchaseProp);
		return Result.OK(itemPurchaseProp);
	}
	
	@DeleteMapping("remove/{propType}/{code}")
	@ApiOperation(value = "删除物料采购属性", notes = "删除物料采购属性", httpMethod = "DELETE")
	public Result<ItemPurchaseProp> remove(@PathVariable(name = "propType", required = true) String propType,
			@PathVariable(name = "code", required = true) String code) {
		ItemPropKey key = new ItemPropKey();
		key.setPropType(propType);
		key.setCode(code);
		itemPurchasePropSerivce.delete(ItemPurchaseProp.class, key);
		return Result.OK();
	}
	
}
